#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include "../include/ipc.h"
#include "../include/unicom.h"
#include "../include/advance_setting.h"

#include <sqlite3.h>

#define SQLITE_ADVANCE "warehouse.db"

void init_advance_msg(struct ipc_info *p_ipc_info)
{
	if ((p_ipc_info->a_key = ftok("/app", 'g')) < 0)
	{
		perror("init_advance_msg, ftok");
		exit(-1);
	}

	if ((p_ipc_info->a_msgid = msgget(p_ipc_info->a_key, IPC_EXCL)) < 0)
	{
		fprintf(stderr, "message queue %#x was not exist !\n", p_ipc_info->a_key);
		if ((p_ipc_info->a_msgid = msgget(p_ipc_info->a_key, 0666 | IPC_CREAT)) < 0)
		{
			perror("init_advance_msg, message queue create");
			exit(-1);
		}
		fprintf(stderr, "message queue %#x was create success !\n", p_ipc_info->a_key);
	}
}

/*
 *回调函数
 **/
int a_callback(void *para, int n_column, char **column_value,
			   char **column_name)
{
	int *pInt = (int *)para;
	*pInt = 1;

	return 0;
}

/*
 *消息读取，实现网页高级设置存如数据库
 **/
int get_advance_msg(struct ipc_info *p_ipc_info, struct msg *msg_buf)
{
	int ret;
	struct msg msg_temp;
	memset(&msg_temp, 0, sizeof(struct msg));

	/* 阻塞方式读消息队列 */
	int flag = 0;

#ifdef NOBLOCK
	flag = IPC_NOWAIT;
#endif

	ret = msgrcv(p_ipc_info->a_msgid, &msg_temp, sizeof(struct msg) - sizeof(long), 9L, flag);
	fprintf(stderr, "ret: %d\n", ret);
	if (ret == -1)
	{
		perror("msgrcv");
		return -1;
	}
	if (msg_temp.msgtype == 5L)
	{
		fprintf(stderr, "XXXXXXXX: check: %ld, \n", msg_temp.msgtype);
		memcpy(msg_buf, &msg_temp, sizeof(struct msg));
	}

	fprintf(stderr, "XXXXXXXX: changku: %c, \n", msg_temp.text[0]);

	return 0;
}

/*
 *数据库中的高级设置：设置上下阀值
 **/
int set_advance_info(struct msg *msg_buf)
{
	sqlite3 *db = NULL;
	char *err_msg = NULL;
	int ret = 0;
	char sql[1024] = "NULL";

	//sprintf(sql, "select * from goods where dev_no = '%d' and goodsId = '%d';", storage_no, goods_kinds);
	sprintf(sql, "select * from env where dev_no='%c';", msg_buf->text[0]);

	ret = sqlite3_open(SQLITE_ADVANCE, &db);
	if (ret != SQLITE_OK)
	{
		fprintf(stderr, "sqlite3_open:%s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return -1;
	}
	else
	{
		int select_flag = -999;
		ret = sqlite3_exec(db, sql, a_callback, (void *)(&select_flag), &err_msg);
		if (ret != SQLITE_OK)
		{
			printf("sqlite3_exec:%s\n", sqlite3_errmsg(db));
			sqlite3_close(db);
			return -1;
		}
		else if (select_flag == 1)
		{
			struct setEnv *p_setEnv = (struct setEnv *)(&msg_buf->text[1]);
			char field_name[14][24] = {
				"dev_no", "temperatureMax", "temperatureMin", "humidityMax",
				"humidityMin", "illuminationMax", "illuminationMin", "telephoneNum", "mode",
				"ifnocice", "interval", "updatePeriod", "username", "password"};

			int setval[14] = {1, p_setEnv->temMAX, p_setEnv->temMIN, p_setEnv->humMAX,
							  p_setEnv->humMIN, p_setEnv->illMAX, p_setEnv->illMIN}; //从网页获取的输入值,用作新的设置值

			char equation[1024] = {0};	//公式字符串
			char sql_update[512] = {0}; //sql语句
			int i;

			for (i = 1; i < 7; i++)
			{
				sprintf(equation, "%s%s=%d,", equation, field_name[i], setval[i]);
			}
			equation[strlen(equation) - 1] = '\0';

			sprintf(sql_update, "update env set %s where dev_no='%c';", equation, msg_buf->text[0]);

			exec_sql_no_callback(db, sql_update);
			puts(sql_update);
		}
		else
		{
			fprintf(stderr, "DataBase Update Table Error, Have no such table where dev_no='%c'!", msg_buf->text[0]);
		}
	}
	sqlite3_close(db);
	return 0;
}

/*
 *sql执行
 **/
int exec_sql_no_callback(sqlite3 *db, const char *sql)
{
	char *err_msg = NULL;
	int ret = 0;

	ret = sqlite3_exec(db, sql, NULL, NULL, &err_msg);
	if (ret != SQLITE_OK)
	{
		printf("sqlite3_exec:%s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return -1;
	}
	return 0;
}
